<?php


class WaYangPay
{
    private $mchId;
    private $secret;
    public function __construct()
    {
        $this->mchId = "";
        $this->secret="";
    }

    /**
     * 生成签名
     * @param array $data 待签名数据
     * @return string
     */
    private function getSign($data)
    {
        $signPars = "";
        ksort($data);
        foreach ($data as $k => $v) {
            if ("sign" != $k) { // && "" != $v && $v != "0"
                $signPars .= $k . "=" . $v . "&";
            }
        }
        $signPars .= "key=" . $this->secret;
        return strtoupper(md5($signPars));
    }

    /**
     * 使用curl发送
     * @param string $url
     * @param mixed $param
     * @return false|mixed
     */
    public function requestPost($url, $param)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSLVERSION, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        $data = curl_exec($ch);
        curl_close($ch);
        return json_decode($data, true);
    }

    /**
     * @Notes: 创建H5支付单
     * @param $mchId
     * @param $mobilePhone
     * @param $amount
     * @param $goodsInfo
     * @param $orderNo
     * @param $callbackUrl
     * @param $notifyUrl
     * @Date: 2021/01/25 13:58
     */
    public function creatOrder($mobilePhone, $amount, $goodsInfo, $orderNo, $callbackUrl, $notifyUrl)
    {
        $url = "https://wayangpay.co.id/payer-api/pay-h5";
        $data = [
            "mchId" => $this->mchId,
            "mobilePhone" => $mobilePhone,
            "amount" => $amount,
            "callbackUrl" => $goodsInfo, //$NotifyUrl
            "goodsInfo" => $orderNo,
            "orderNo" => $callbackUrl,
            "notifyUrl" => $notifyUrl // $ReturnUrl
        ];
        $data['sign'] = $this->getSign($data);
        $res = $this->requestPost($url, $data);
        print_r($res);
    }
}